<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Zhuoyi
 *
 * @author dragon.hu add @2016.2.19
 */
require_once 'BaseChannel.php';

//卓易
class Zhuoyi implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="7de6ce74782473fddd8dab875c85d0e8"; //app_server_key
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("zhuoyi", $_REQUEST);
		/*
		$Recharge_Id = $_REQUEST['Recharge_Id']; //卓悠订单流水号
		$App_Id = $_REQUEST['App_Id']; //应用Id
		$Uin = $_REQUEST['Uin']; //充值用户Id
		$Urecharge_Id = $_REQUEST['Urecharge_Id']; //CP订单流水号
		$Extra = $_REQUEST['Extra']; //附加信息
		$Recharge_Money = $_REQUEST['Recharge_Money']; //订单金额
		$Recharge_Gold_Count = $_REQUEST['Recharge_Gold_Count']; //
		$Pay_Status = $_REQUEST['Pay_Status']; //状态(-1取消交易，0未操作，1支付成功，2支付失败)
		$Create_Time = $_REQUEST['Create_Time']; //订单创建时间
		$sign = $_REQUEST['Sign']; //签名
		*/
		
		if($this->verifySignature($_REQUEST, $paymentKey, 'MD5'))  
		{ //验证通过,进行发货
			$tmpstatus = STATUS_SUCCESS;
			if (1 != $_REQUEST['Pay_Status'])//状态(-1取消交易，0未操作，1支付成功，2支付失败)
			{
				$tmpstatus = STATUS_FAILD;
			}
			
			if(false == $orderLog->recordOrder($tmpstatus, $_REQUEST['Recharge_Money'], $_REQUEST['Urecharge_Id'], "zhuoyi", $platform,$_REQUEST['Recharge_Id']))
			{
				$ret="failure";
				$orderLog->log("zhuoyi",array("order"=>$_REQUEST['Urecharge_Id'],"money"=>$_REQUEST['Recharge_Money'],"serverresult"=>"faild"));
			}
			else 
			{
				//支付成功
				$ret="success";
				$orderLog->log("zhuoyi",array("order"=>$_REQUEST['Urecharge_Id'],"money"=>$_REQUEST['Recharge_Money'],"serverresult"=>"success"));
			}
		} 
		else 
		{
			$orderLog->log("zhuoyi","sign error!");
			$ret =  "failure";
		}
		echo $ret;
	}
	/***************************卓易提供的接口******************************/
	function verifySignature($p_data, $p_key, $p_sign_type='MD5') {
	
		$result_sign   		= trim($p_data['Sign']);
		$delete_sign   		= $this->paraFilter($p_data);
		$arg_sort      		= $this->arg_sort($delete_sign);
		$return_string 		= $this->createLinkString($arg_sort);
		$sign_return_string = $this->buildMysign($return_string, $p_key, $p_sign_type);
		if ($result_sign === $sign_return_string) {
			return true;
		} else {
			return false;
		}
	}
	
	function paraFilter($p_data) {
		$para = array();
		while (list ($key, $val) = each ($p_data)) {
			if($key == "Sign" || $key === '') continue;
			else $para[$key] = $p_data[$key];
		}
		return $para;
	}
	
	function arg_sort($array) {
		ksort($array);
		reset($array);
		return $array;
	}
	
	/*
	 * @description 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
	 * @param  array  $p_array
	 * @return string $data
	 *
	 */
	function createLinkString($p_array) {
		$arg  = "";
		while (list ($key, $val) = each ($p_array)) {
			$arg.=$key."=".$val."&";
		}
		$data = substr($arg,0,count($arg)-2);		     //去掉最后一个&字符
		return $data;
	}
	
	function buildMysign($p_string, $p_key, $p_sign_type = 'MD5') {
	
		$data = $p_string . $p_key;
	
		if($p_sign_type == 'MD5') {
			$data = md5($data);
		} elseif($p_sign_type =='DSA') {
			//TODO 后续开发
		} else {
			//无效签名
			$data = false;
		}
	
		$data = urlencode($data);
		return $data;
	}
	/***********************接口结束*************************/
}